package com.suray.system.run.data.mysql.analyze;

import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.system.run.data.mysql.device.charge.ChargeInfoDBServiceImpl;
import com.suray.system.run.data.mysql.device.entity.ChargeInfoDB;
import com.suray.system.run.data.mysql.device.entity.LowBatteryDB;
import com.suray.system.run.type.DeviceType;

import java.util.Date;
import java.util.List;

public class ChargeInfoDataStore {
    private ChargeInfoDBServiceImpl chargeInfoDBService = SpringContextHolder.getBean(ChargeInfoDBServiceImpl.class);

    /**
     * 设备序列号
     */
    private String series;

    /**
     * 当前任务表
     */
    private ChargeInfoDB chargeInfoDB;

    /**
     * 设备类型
     */
    private DeviceType deviceType;

    public ChargeInfoDataStore(String series, DeviceType deviceType) {
        this.series = series;
        this.deviceType = deviceType;
    }

    /**
     * 查找小车当前充电信息
     * @return
     */
    public ChargeInfoDB findChargeInfoData() {
        chargeInfoDB = chargeInfoDBService.findChargeInfoDBBySeries(series);
        return chargeInfoDB;
    }

    /**
     * 创建小车当前充电信息
     * @return
     */
    public ChargeInfoDB createChargeInfoData() {
        chargeInfoDB = new ChargeInfoDB();
        chargeInfoDB.setSeries(series);
        chargeInfoDB.setDeviceType(deviceType);
        chargeInfoDB.setChargeStartTime(new Date(System.currentTimeMillis()));
        chargeInfoDBService.save(chargeInfoDB);
        return chargeInfoDB;
    }

    /**
     * 更新小车当前充电信息
     * @return
     */
    public boolean updateChargeInfoData() {
        return chargeInfoDBService.updateById(chargeInfoDB);
    }

    /**
     * 获取小车所有充电信息
     * @param startTime
     * @param endTime
     * @return
     */
    public List<ChargeInfoDB> getChareInfoList(Date startTime, Date endTime) {
        return chargeInfoDBService.getChareInfoList(series, startTime, endTime);
    }

    /**
     * 获取小车充电次数
     * @return
     */
    public int getChargeNum() {
        return chargeInfoDBService.count();
    }
}
